package cn.datawin.cmppserver.util;

import cn.datawin.bms.customer.redis.CstmCustomerCmppRedisDto;
import cn.datawin.common.core.util.RedisUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.datawin.constant.RedisPattern.CSTM_CUSTOMER_CMPP_CACHE;


@Service
public class CacheService {

    public List<CstmCustomerCmppRedisDto> getAllValidCustomer() {
		String replace = CSTM_CUSTOMER_CMPP_CACHE.replace("%s", "*");
		Collection<String> keys = RedisUtils.findKeysForPage(replace, 0, 1000000);
        List<CstmCustomerCmppRedisDto> cstmCustomerCmppRedisDtos = new ArrayList<>();
        for (String key : keys) {
            CstmCustomerCmppRedisDto cstmCustomerCmppRedisDto = RedisUtils.get(key);
            if (cstmCustomerCmppRedisDto != null) {
                cstmCustomerCmppRedisDtos.add(cstmCustomerCmppRedisDto);
            }
        }

        return cstmCustomerCmppRedisDtos;
    }

    public void updateConnections(Long id, String s) {
    }

    public CstmCustomerCmppRedisDto getCustomer(String userName) {
        String redisKey = String.format(CSTM_CUSTOMER_CMPP_CACHE, userName);
        return  RedisUtils.get(redisKey);
    }
}
